package com.hmkj.core.mapper.amount;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.amount.AccountSimulateModel;
import com.hmkj.core.po.amount.AccountSimulate;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 模拟账户表相关Dao接口<br>
 *
 * @author eproo
 * @date   2017-11-19 05:19:03
 */
public interface AccountSimulateMapper extends GenericMapper< AccountSimulate,Integer> {

    @Select("SELECT count(1) FROM fm_account_simulate t LEFT JOIN biz_user u ON t.user_id = u.id  WHERE 1=1 ${whereSql} ${sortSql} ")
    long countBySqlJoin(Map<String, Object> params);

    @Select("SELECT t.*,u.user_name AS 'u.userName',u.mobile AS 'u.mobile' FROM fm_account_simulate t LEFT JOIN biz_user u ON t.user_id = u.id WHERE 1=1 ${whereSql} ${sortSql} limit ${nowPage}, ${pageSize} ")
    List<AccountSimulateModel> findListBySqlJoin(Map<String, Object> params);

    /**
     * 根据用户ID 查询用户账户信息
     *
     * @param userId 用户ID
     * @return
     */
    @Select("SELECT * FROM fm_account_simulate   WHERE user_id = #{userId} and del_flag = 1")
    AccountSimulate selectByUID(@Param(value = "userId") int userId);

    /**
     * 根据用户ID 更新用户模拟账户资金
     *
     * @param total     总额变化量
     * @param available 可用变化量
     * @param freeze    冻结变化量
     * @param userId    用户ID
     */
    @Update("UPDATE fm_account_simulate SET total = total + (#{total}), available = available + (#{available}), freeze = freeze + (#{freeze})," +
            "update_time = now() WHERE  user_id = #{userId} AND total + (#{total}) >= 0 AND available + (#{available}) >= 0 AND freeze + (#{freeze}) >= 0")
    int updateAccountSimulate(@Param(value = "total") BigDecimal total,
                      @Param(value = "available") BigDecimal available,
                      @Param(value = "freeze") BigDecimal freeze,
                      @Param(value = "userId") Integer userId);

    /**
     * 查询符合条件的投资人账户（账户可用金额大于等于止盈保证金）
     * @param profitDeposit 止盈保证金
     * @return
     */
    @Select("SELECT a.* FROM fm_account_simulate a LEFT JOIN biz_investor_simulate i ON a.user_id = i.user_id WHERE ISNULL(i.id)!=1 AND i.status = 1 AND a.available >= #{profitDeposit} AND  a.del_flag = 1")
    List<AccountSimulate> selectInvestAccountSimulate(@Param(value = "profitDeposit") BigDecimal profitDeposit);

    /**
     * 查询符合条件的投资人账户（符合当前代理商下的模拟投资人）
     * @param agentId 代理商Id
     * @return
     */
    @Select("SELECT a.* FROM fm_account_simulate a LEFT JOIN biz_investor_simulate i ON a.user_id = i.user_id LEFT JOIN biz_user u ON i.user_id = u.id WHERE ISNULL(i.id)!=1 AND i.status = 1 AND  a.del_flag = 1 AND u.id = 1 AND u.agent_id = #{agentId} ")
    List<AccountSimulate> selectByAgentId(@Param(value = "agentId") Integer agentId);
}